/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.tools;

import java.io.IOException;
import java.math.BigDecimal;
import java.security.MessageDigest;
import org.apache.commons.codec.binary.Hex;

/**
 *
 * @author lenovo-pc
 */
public class SHA {

	public static void main(String[] args) throws IOException {

		String ch = "123";
		String spec = "11";
		String key = "222sadadsadddddddddddddddddddddddddddddddddddddddddddddddddddddddd";
		System.out.println(key.hashCode());
		//System.out.println(hexString2binaryString(SHA256(key)));
//		System.out.println(SHA256(key));
//		System.out.println(hexString2binaryString(SHA256(key)));
//		System.out.println(Two2Ten(hexString2binaryString(SHA256(key))));
		System.out.println("ABS HASH CODE:"+Math.abs(Long.valueOf(LongHashCode.hashCode(key))));
		System.out.println("HASH CODE :"+LongHashCode.hashCode(key));
		System.out.println("MD5 :"+GenerateMD5.string2MD5(key));
		System.out.println("MD5 hashcode:"+LongHashCode.hashCode(GenerateMD5.string2MD5(key)));
		System.out.println(LongHashCode.readUnsignedLong(Long.valueOf(LongHashCode.hashCode(GenerateMD5.string2MD5(key)))));
		Long l=LongHashCode.readUnsignedLong(Long.valueOf(LongHashCode.hashCode(GenerateMD5.string2MD5(key)))).longValue();
		System.out.println(Long.MAX_VALUE);
		
	}


	public static String SHA256(String source) {
		try {
			MessageDigest digest = MessageDigest.getInstance("SHA-256");
			byte[] hash = digest.digest(source.getBytes("UTF-8"));
			return Hex.encodeHexString(hash);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	public static String hexString2binaryString(String hexString) {
        if (hexString == null) {
            return null;
        }
        String bString = "", tmp;
        for (int i = 0; i < hexString.length(); i++) {
            tmp = "0000"
                    + Integer.toBinaryString(Integer.parseInt(hexString
                            .substring(i, i + 1), 16));
            bString += tmp.substring(tmp.length() - 4);
        }
        return bString;
    }
	
    public static String Two2Ten(String two) {
       // String two = "101010100101010";
        int k = 0;
        
        for(int i=0;i<two.length();++i){
            char tmp = two.charAt(i);
            int intTmp = tmp-48;
            k = k*2+intTmp;
       
    }
		String kString=String.valueOf(k);
		return kString;
    
}
}
